{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "import numpy as np\n",
    "import tensorflow as tf\n",
    "from sklearn.feature_extraction.text import TfidfVectorizer\n",
    "from gensim.models.word2vec import Word2Vec\n",
    "from sklearn.preprocessing import LabelEncoder"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 这是第一个函数 用于获取`source_string`和`target_string`两个列表。     \n",
    "这两个列表的数据为:    \n",
    "    每个文件里面的词按空格分隔后，组成的列表；然后每个列表在组成的列表，这个列表的长度为8"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 通过文件夹名获取两个列表，中的数据为\n",
    "def get_train_list(source_folder, target_folder):\n",
    "    source_string = []\n",
    "    target_string = []\n",
    "    # os.listdir 用于返回指定的文件夹包含的文件或文件夹的名字的 列表\n",
    "    for filename in os.listdir(source_folder):\n",
    "        # 把未标注文本的文件名拼接为和目标文本一样的文件名\n",
    "        target_file_name = \"targetH_\" + \"_\".join(filename.split(\"_\")[1:])\n",
    "        # os.path.exists 如果path存在，返回True；如果path不存在，返回False。 \n",
    "        # os.path.join 用于拼接文件路径\n",
    "        # 检查 拼好的这个文件名在目标文件里是否存在\n",
    "        if os.path.exists(os.path.join(target_folder, target_file_name)):\n",
    "            # 打开 源文件 和 目标文件\n",
    "            with open(os.path.join(source_folder, filename), 'r', encoding=\"utf-8\") as source:\n",
    "                with open(os.path.join(target_folder, target_file_name), 'r', encoding=\"utf-8\") as target:\n",
    "                    # zip 返回 两个参数对应位置拼成一个元组 的一个由元组组成的列表\n",
    "                    # 这里用zip我认为就是为了把两个内容写进一个for里 方便比较\n",
    "                    for source_line, target_line in zip(source.readlines(), target.readlines()):  \n",
    "                        s_line = source_line.split() # source_line 是一个str，按空格分割一下\n",
    "                        t_line = target_line.split()\n",
    "                        # 如果长度相等代表标注正确 \n",
    "                        if len(s_line) == len(t_line):\n",
    "                            source_string.append(s_line)\n",
    "                            target_string.append(t_line)\n",
    "    print('源数据读取完毕，共' + str(len(source_string)) + '行')\n",
    "    return source_string, target_string"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['初见', '安迪苏', '(', 'SH', '：', '600299', ')', '是', '前', '阵子', '看到', '这样', '一则', '新闻', '：6', '月', '30', '日讯', '，', '坊间', '传闻', '近两年', '的', '中国', '化工集团', '与', '中化', '集团', '合', '并', '交易', '终于', '坐实', '，', '中国', '化工', '董事长', '任建新', '宣布', '退休', '，', '中化', '集团董事长', '宁高宁', '担任', '合', '并', '后', '公司', '的', '董事长', '，', '中组部', '和', '国资委', '相关', '负责人', '到', '中国', '化工集团', '宣布', '了', '上述', '公司', '合', '并', '与', '人事安排', '。', '（', '财新网', '）根据', '公开', '信息', '可知', '，', '中国', '中化', '集团公司', '和', '中国', '化工', '集团公司', '旗下', '上市公司', '包括', '：', '中化国际', '(', 'SH', ':', '600500', ')', '、', '沈阳化工', '(', 'SZ', ':', '000698', ')', '、', '风神股份', '(', 'SH', ':', '600469', ')', '、', '江山股份', '(', 'SH', ':', '600389', ')', '、', '天科股份', '(', 'SH', ':', '600378', ')', '、', '扬农化工', '(', 'SH', ':', '600486', ')', '、', '沧州大化', '(', 'SH', ':', '600230', ')', '、', '天华院', '(', 'SH', ':', '600579', ')', '、', '安迪苏', '等', '。这', '其中', '提到', '的', '公司', '中', '，', '我', '只', '研究', '并', '买', '过', '扬农化工', '（', '目前', '已', '清仓', '）', '，', '其他', '如', '中化国际', '、', '江山', '等', '也', '看过', '一些', '，', '不太敢', '买', '。', '唯独', '这个', '安迪苏', '被', '我', '忽略', '掉', '了', '。这', '两', '天花', '时间', '读', '了', '安迪苏', '近些年', '的', '财报', '，', '不看', '不', '知道', '，', '一看', '吓一跳', '。', '那', '这个', '起着', '外资', '名字', '的', '企业', '到底', '是', '个', '怎样', '的', '企业', '呢', '？', '我', '简要', '分享', '几点', '重要信息', '抛砖引玉', '供', '大家', '参考', '。这个', '公司', '卖', '啥', '的', '，', '有', '替代', '风险', '吗', '？公司目前', '主营', '核心', '业务', '是', '功能性', '产品', '，', '营收', '占', '比', '77.92%', '，', '不过', '利润', '占', '比', '差不多', '69%', '。', '利润', '占', '比', '低于', '营收', '占', '比', '的', '主要', '原因', '是', '特种', '产品', '的', '高', '利润', '，', '这个', '后面', '再谈', '。那', '这个', '功能性', '产品', '是', '啥', '呢', '？', '我', '查询', '了', '年报', '，', '功能性', '产品', '指', '的', '是', '—', '—', '蛋氨酸', '、', '蛋氨酸', '羟基', '类似物', '、', '维生素', '、', '硫酸铵', '、', '硫酸钠', '。', '而', '占', '功能', '产品', '核心', '的', '就是', '蛋氨酸', '产品', '（', '包括', '固态', '和', '液态', '的', '）', '。那', '这个', '蛋氨酸', '又', '是', '干嘛', '的', '呢', '？', '简单', '点', '说', '，', '就是', '动物', '饲养', '中', '的', '营养', '添加剂', '。']\n"
     ]
    }
   ],
   "source": [
    "source_folder = 'data_命名实体抽取/split'\n",
    "target_folder = 'data_命名实体抽取/targetH'\n",
    "splitList = os.listdir(source_folder)\n",
    "splitList\n",
    "filename = splitList[0]\n",
    "target_file_name = \"targetH_\" + \"_\".join(filename.split(\"_\")[1:])\n",
    "\n",
    "jointPath = os.path.join(source_folder, target_file_name)\n",
    "with open(os.path.join(source_folder, filename), 'r', encoding=\"utf-8\") as source:\n",
    "    with open(os.path.join(target_folder, target_file_name), 'r', encoding=\"utf-8\") as target:\n",
    "        for source_line, target_line in zip(source.readlines(), target.readlines()):  \n",
    "            s_line = source_line.split() # source_line 是一个str，按空格分割一下\n",
    "            t_line = target_line.split()\n",
    "            print(s_line)\n",
    "#                         # 如果长度相等代表标注正确 \n",
    "# source_string, target_string = get_train_list(source_folder,target_folder)\n",
    "# len(source_string)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 第四个函数 用语料训练好的模型训练词向量\n",
    "参数为 第一个就相当于X 模型 10个词 多的去掉 少的补零"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [],
   "source": [
    "def get_train_feature(source_string, vec_model, embedding_size=128, max_sequence=10):\n",
    "    # 对所有训练了的词进行去重\n",
    "    index2word_set = set(vec_model.wv.index2word)\n",
    "    row_vector_list = []\n",
    "    for source_line in source_string:\n",
    "    # 遍历的每个元素 是每个文件的词所组成的列表\n",
    "        i = 0\n",
    "        row_vector = []\n",
    "        # 遍历每个列表中的每个词\n",
    "        for source_word in source_line:\n",
    "            if i < max_sequence:\n",
    "                if source_word in index2word_set:\n",
    "                    #将values插入到目标arr的最后 默认接到行后面\n",
    "                    row_vector= np.append(row_vector, vec_model[source_word])\n",
    "                else:\n",
    "                    row_vector = np.append(row_vector, np.zeros(embedding_size, dtype='float32'))\n",
    "            i += 1\n",
    "            # 加到10个为止\n",
    "        # 如果小于10个 就补零\n",
    "        if len(source_line) < max_sequence:\n",
    "            row_vector = np.append(row_vector,\n",
    "                                   np.zeros((embedding_size * (max_sequence - len(source_line)),),\n",
    "                                            dtype='float32'))\n",
    "        row_vector_list.append(row_vector)\n",
    "    return np.matrix(row_vector_list,dtype='float32')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 这是第二个函数 用来生成语料"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [],
   "source": [
    "def read_file_to_corpus(folder):\n",
    "    corpus = []\n",
    "    for filename in os.listdir(folder):\n",
    "        with open(os.path.join(folder, filename), encoding=\"utf-8\") as f:\n",
    "            for line in f:\n",
    "                corpus.append(line.split())\n",
    "    return corpus"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 这是第三个函数 用来训练模型      \n",
    "参数为 语料 训练的词向量的维数        \n",
    "是对词进行过滤，频率小于min-count的单词则会被忽视，默认值为5    \n",
    "保存路径"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [],
   "source": [
    "def get_vec_from_corpus(corpus, size=128, min_count=2, save_path=os.path.join('NER_FOLDER', \"ner_word2vec_model\")):\n",
    "    vec_model = Word2Vec(corpus, size, min_count)\n",
    "    vec_model.save(save_path)\n",
    "    return vec_model"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 第五个函数  获取训练集标签\n",
    "参数就为`target_string`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [],
   "source": [
    "def get_target_label(target_string,max_sequence=10):\n",
    "    onehot_model = OneHot()\n",
    "    # 列表长度为文件个数\n",
    "    for i in range(0, len(target_string)):\n",
    "        # 如果 文件里面的标注个数小于 10 差多少补多少\n",
    "        if len(target_string[i]) < max_sequence:\n",
    "            target_string[i] = target_string[i].extend([\"O\"]*(max_sequence - len(target_string[i])))\n",
    "            if target_string[i] is None:\n",
    "                target_string[i] = [\"O\"]*max_sequence\n",
    "        # 大于10 取前十个\n",
    "        else:\n",
    "            if target_string[i] is None:\n",
    "                target_string[i] = [\"O\"]*max_sequence\n",
    "            else:\n",
    "                target_string[i] = target_string[i][0:max_sequence]\n",
    "    num_rows = len(target_string)\n",
    "    flat_list = [item for sublist in target_string for item in sublist]\n",
    "    print(flat_list)\n",
    "    target_vector = onehot_model.encode_label(flat_list)\n",
    "    print(target_vector)\n",
    "    target_vector = target_vector.reshape(-1, max_sequence)\n",
    "    print(target_vector)\n",
    "    return target_vector, onehot_model"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [],
   "source": [
    "class OneHot(object):\n",
    "    def __init__(self):\n",
    "        self.__label_encoder = LabelEncoder()\n",
    "\n",
    "    def encode_label(self, target_list):\n",
    "        integer_encoded = self.__label_encoder.fit_transform(np.array(target_list))\n",
    "        return integer_encoded\n",
    "\n",
    "    def decode(self, encoder_list):\n",
    "        return self.__label_encoder.inverse_transform([np.argmax(np.array(encoder_list), axis=1)])\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "源数据读取完毕，共8行\n",
      "['O', 'B-ORG-A', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'B-ORG-A', 'I-ORG-A', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'B-ORG-O', 'I-ORG-O', 'O', 'O', 'O', 'B-ORG-O', 'O', 'B-ORG-O', 'I-ORG-O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O']\n",
      "[4 0 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 0 2 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4\n",
      " 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 1 3 4 4 4 1 4 1 3 4 4 4 4\n",
      " 4 4 4 4 4 4]\n",
      "[[4 0 4 4 4 4 4 4 4 4]\n",
      " [4 4 4 4 4 4 4 4 0 2]\n",
      " [4 4 4 4 4 4 4 4 4 4]\n",
      " [4 4 4 4 4 4 4 4 4 4]\n",
      " [4 4 4 4 4 4 4 4 4 4]\n",
      " [4 4 4 4 4 4 4 4 4 4]\n",
      " [4 1 3 4 4 4 1 4 1 3]\n",
      " [4 4 4 4 4 4 4 4 4 4]]\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "D:\\ProgramingTools\\Anaconda\\lib\\site-packages\\ipykernel_launcher.py:14: DeprecationWarning: Call to deprecated `__getitem__` (Method will be removed in 4.0.0, use self.wv.__getitem__() instead).\n",
      "  \n"
     ]
    }
   ],
   "source": [
    "# 先用两个语意变量把路径存起来\n",
    "source_folder = 'data_命名实体抽取/split'\n",
    "target_folder = 'data_命名实体抽取/targetH'\n",
    "#调用第一个函数 get_train_list 获取 训练样本 包含 原文（要训练的X）、目标值（y），用两个变量存起来\n",
    "train_X, train_y = get_train_list(source_folder,target_folder)\n",
    "# 调用第二个函数 生成语料 用一个变量存起来\n",
    "corpus_train = read_file_to_corpus(source_folder)\n",
    "# 调用第三个函数 训练模型并保存\n",
    "model = get_vec_from_corpus(corpus_train)\n",
    "# 调用第四个函数 获取训练集词向量\n",
    "mat = get_train_feature(train_X, model)\n",
    "# 调用第五个函数 获取训练集标签\n",
    "tar_vec,oneHot = get_target_label(train_y)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(matrix([[  0.       ,   0.       ,   0.       , ...,   0.       ,\n",
       "            0.       ,   0.       ],\n",
       "         [ -1.4712857,  -1.6622865,   1.5915074, ...,  -1.6911398,\n",
       "            2.9176981,   1.47905  ],\n",
       "         [ -8.031942 , -11.217584 ,  10.41392  , ...,   3.2312264,\n",
       "           -8.402367 ,  -7.1537557],\n",
       "         ...,\n",
       "         [  0.       ,   0.       ,   0.       , ...,   0.       ,\n",
       "            0.       ,   0.       ],\n",
       "         [  0.       ,   0.       ,   0.       , ...,   0.       ,\n",
       "            0.       ,   0.       ],\n",
       "         [  0.       ,   0.       ,   0.       , ...,  -5.607462 ,\n",
       "            8.091679 ,   3.0654242]], dtype=float32),\n",
       " array([[4, 0, 4, 4, 4, 4, 4, 4, 4, 4],\n",
       "        [4, 4, 4, 4, 4, 4, 4, 4, 0, 2],\n",
       "        [4, 4, 4, 4, 4, 4, 4, 4, 4, 4],\n",
       "        [4, 4, 4, 4, 4, 4, 4, 4, 4, 4],\n",
       "        [4, 4, 4, 4, 4, 4, 4, 4, 4, 4],\n",
       "        [4, 4, 4, 4, 4, 4, 4, 4, 4, 4],\n",
       "        [4, 1, 3, 4, 4, 4, 1, 4, 1, 3],\n",
       "        [4, 4, 4, 4, 4, 4, 4, 4, 4, 4]], dtype=int64))"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "mat,tar_vec"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(8, 1280)"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "mat.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['O', 'B-ORG-A', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'B-ORG-A', 'I-ORG-A', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'B-ORG-O', 'I-ORG-O', 'O', 'O', 'O', 'B-ORG-O', 'O', 'B-ORG-O', 'I-ORG-O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O']\n",
      "[4 0 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 0 2 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4\n",
      " 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 1 3 4 4 4 1 4 1 3 4 4 4 4\n",
      " 4 4 4 4 4 4]\n",
      "[[4 0 4 4 4 4 4 4 4 4]\n",
      " [4 4 4 4 4 4 4 4 0 2]\n",
      " [4 4 4 4 4 4 4 4 4 4]\n",
      " [4 4 4 4 4 4 4 4 4 4]\n",
      " [4 4 4 4 4 4 4 4 4 4]\n",
      " [4 4 4 4 4 4 4 4 4 4]\n",
      " [4 1 3 4 4 4 1 4 1 3]\n",
      " [4 4 4 4 4 4 4 4 4 4]]\n"
     ]
    }
   ],
   "source": [
    "tar_vec,onehot_model = get_target_label(train_y)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [],
   "source": [
    "embedding_size = 128\n",
    "unit_num = 128\n",
    "dropout_rate = None\n",
    "output_size = 5\n",
    "batch_size = 1\n",
    "seq_length = 10\n",
    "lr = 0.001"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 用tensorflow实现双向LSTM"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {},
   "outputs": [],
   "source": [
    "def lstm_crf(X):\n",
    "    # rnn_cell 是一个model 里面包含了 BasicLSTMCell \n",
    "    cell_forward = tf.nn.rnn_cell.BasicLSTMCell(unit_num)\n",
    "    cell_backward = tf.nn.rnn_cell.BasicLSTMCell(unit_num)\n",
    "\n",
    "    if dropout_rate is not None:\n",
    "        # Dropout 使一部分神经元停止工作\n",
    "        cell_forward = DropoutWrapper(cell_forward, input_keep_prob=1.0, output_keep_prob=dropout_rate)\n",
    "        cell_backward = DropoutWrapper(cell_backward, input_keep_prob=1.0, output_keep_prob=dropout_rate)\n",
    "\n",
    "    input_bi_lstm = tf.reshape(X, [batch_size, seq_length, embedding_size])\n",
    "    bi_outputs, bi_state = tf.nn.bidirectional_dynamic_rnn(cell_forward,\n",
    "                                    cell_backward, input_bi_lstm, dtype=tf.float32)\n",
    "\n",
    "    bi_output = tf.concat(bi_outputs, axis=2)\n",
    "\n",
    "    W = tf.get_variable(\"projection_w\", [2 * unit_num, output_size])\n",
    "    b = tf.get_variable(\"projection_b\", [output_size])\n",
    "    x_reshape = tf.reshape(bi_output, [-1, 2 * unit_num])\n",
    "    projection = tf.matmul(x_reshape, W) + b\n",
    "    outputs = tf.reshape(projection, [batch_size, seq_length, output_size])\n",
    "    return outputs"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "为什么设batch？\n",
    "因为设备不够 所以用随机梯度下降"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [],
   "source": [
    "def train(feature, target, save_path, iter_num):\n",
    "        sess = tf.Session()\n",
    "        \n",
    "        # 创建 \n",
    "        X = tf.placeholder(tf.float32, shape=[batch_size, seq_length*embedding_size])\n",
    "        Y = tf.placeholder(tf.int32, shape=[batch_size, seq_length])\n",
    "        # 预测值\n",
    "        pred = lstm_crf(X)\n",
    "        real_y = tf.reshape(Y, [batch_size, seq_length])\n",
    "        print(batch_size * [seq_length])\n",
    "        # log似然求最小化\n",
    "        log_likelihood, transition_params =\\\n",
    "        tf.contrib.crf.crf_log_likelihood(pred,\n",
    "                                          real_y, \n",
    "                                          tf.convert_to_tensor(batch_size * [seq_length], dtype=tf.int32))\n",
    "\n",
    "        # 优化\n",
    "        # Add a training op to tune the parameters.\n",
    "        loss = tf.reduce_mean(-log_likelihood)\n",
    "        train_op = tf.train.AdamOptimizer(learning_rate=lr).minimize(loss)\n",
    "        saver = tf.train.Saver(tf.global_variables(), max_to_keep=15)\n",
    "        sess.run(tf.global_variables_initializer())\n",
    "        total_loss = []\n",
    "        iterate = []\n",
    "        for i in range(iter_num):\n",
    "            #total_loss = []\n",
    "            for step in range(int(feature.shape[0]/batch_size)-1):\n",
    "                tf_unary_scores, tf_transition_params, _, loss_ = sess.run([pred, transition_params, train_op, loss],\n",
    "                                                                           feed_dict={X: feature[step*batch_size:(step+1)*batch_size],\n",
    "                                                                                      Y: target[step*batch_size:(step+1)*batch_size]})\n",
    "            total_loss.append(loss_)\n",
    "            iterate.append(i)\n",
    "            if i % 10 == 0:\n",
    "                print(\"迭代第：\" + str(i) + \"次， Loss为：\" + str(loss_))\n",
    "                \n",
    "            if i % 20 == 0:\n",
    "                print(\"保存模型：\", saver.save(sess, save_path, global_step=i))\n",
    "        return total_loss, iterate"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[10]\n",
      "迭代第：0次， Loss为：12.937878\n",
      "保存模型： ./NER_FOLDER/lstm_crf-0\n",
      "迭代第：10次， Loss为：10.426598\n",
      "迭代第：20次， Loss为：6.2886887\n",
      "保存模型： ./NER_FOLDER/lstm_crf-20\n",
      "迭代第：30次， Loss为：2.981861\n",
      "迭代第：40次， Loss为：1.977642\n",
      "保存模型： ./NER_FOLDER/lstm_crf-40\n",
      "迭代第：50次， Loss为：1.3802986\n",
      "迭代第：60次， Loss为：0.8601303\n",
      "保存模型： ./NER_FOLDER/lstm_crf-60\n",
      "迭代第：70次， Loss为：0.42858887\n",
      "迭代第：80次， Loss为：0.21839142\n",
      "保存模型： ./NER_FOLDER/lstm_crf-80\n",
      "迭代第：90次， Loss为：0.13897705\n",
      "迭代第：100次， Loss为：0.09977722\n",
      "保存模型： ./NER_FOLDER/lstm_crf-100\n",
      "迭代第：110次， Loss为：0.07640076\n",
      "迭代第：120次， Loss为：0.06106186\n",
      "保存模型： ./NER_FOLDER/lstm_crf-120\n",
      "迭代第：130次， Loss为：0.050266266\n",
      "迭代第：140次， Loss为：0.042316437\n",
      "保存模型： ./NER_FOLDER/lstm_crf-140\n",
      "迭代第：150次， Loss为：0.03624344\n",
      "迭代第：160次， Loss为：0.031448364\n",
      "保存模型： ./NER_FOLDER/lstm_crf-160\n",
      "迭代第：170次， Loss为：0.02759552\n",
      "迭代第：180次， Loss为：0.02444458\n",
      "保存模型： ./NER_FOLDER/lstm_crf-180\n",
      "迭代第：190次， Loss为：0.021812439\n"
     ]
    }
   ],
   "source": [
    "loss_, iterate_ = train(mat,tar_vec,\"./NER_FOLDER/lstm_crf\",200)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[12.937878,\n",
       " 13.8849325,\n",
       " 14.596285,\n",
       " 14.48323,\n",
       " 13.979223,\n",
       " 13.319788,\n",
       " 12.780802,\n",
       " 12.282072,\n",
       " 11.728306,\n",
       " 11.083685,\n",
       " 10.426598,\n",
       " 9.798544,\n",
       " 9.145733,\n",
       " 8.600321,\n",
       " 7.88126,\n",
       " 7.4566784,\n",
       " 6.8174458,\n",
       " 7.0398903,\n",
       " 6.9423275,\n",
       " 5.8366203,\n",
       " 6.2886887,\n",
       " 5.59165,\n",
       " 5.187191,\n",
       " 5.356596,\n",
       " 4.279463,\n",
       " 4.037472,\n",
       " 3.7970352,\n",
       " 3.579115,\n",
       " 3.3414574,\n",
       " 3.1510658,\n",
       " 2.981861,\n",
       " 2.810955,\n",
       " 2.6706886,\n",
       " 2.5289803,\n",
       " 2.4468727,\n",
       " 2.4178276,\n",
       " 2.3956375,\n",
       " 2.780281,\n",
       " 2.7435646,\n",
       " 3.8479652,\n",
       " 1.977642,\n",
       " 2.4462585,\n",
       " 2.3024979,\n",
       " 1.9359779,\n",
       " 1.8475723,\n",
       " 1.6920319,\n",
       " 1.6223526,\n",
       " 1.5597076,\n",
       " 1.5049438,\n",
       " 1.4354401,\n",
       " 1.3802986,\n",
       " 1.3279037,\n",
       " 1.272377,\n",
       " 1.2180138,\n",
       " 1.1658936,\n",
       " 1.1137848,\n",
       " 1.0622406,\n",
       " 1.011322,\n",
       " 0.9603119,\n",
       " 0.9101944,\n",
       " 0.8601303,\n",
       " 0.8111725,\n",
       " 0.7621231,\n",
       " 0.7154045,\n",
       " 0.6675682,\n",
       " 0.6286354,\n",
       " 0.60551834,\n",
       " 0.5453377,\n",
       " 0.49674225,\n",
       " 0.45995712,\n",
       " 0.42858887,\n",
       " 0.39601898,\n",
       " 0.36779022,\n",
       " 0.34235,\n",
       " 0.318573,\n",
       " 0.29746628,\n",
       " 0.27827072,\n",
       " 0.26081467,\n",
       " 0.24521637,\n",
       " 0.23113632,\n",
       " 0.21839142,\n",
       " 0.20693207,\n",
       " 0.19654465,\n",
       " 0.18709564,\n",
       " 0.17844391,\n",
       " 0.17052078,\n",
       " 0.16321564,\n",
       " 0.15646362,\n",
       " 0.15019226,\n",
       " 0.14438248,\n",
       " 0.13897705,\n",
       " 0.13392258,\n",
       " 0.12918091,\n",
       " 0.12472916,\n",
       " 0.12053299,\n",
       " 0.11657715,\n",
       " 0.11285019,\n",
       " 0.109298706,\n",
       " 0.10596466,\n",
       " 0.10278702,\n",
       " 0.09977722,\n",
       " 0.09690094,\n",
       " 0.09418106,\n",
       " 0.09157562,\n",
       " 0.0891037,\n",
       " 0.08675003,\n",
       " 0.08449173,\n",
       " 0.08234024,\n",
       " 0.08026886,\n",
       " 0.07828903,\n",
       " 0.07640076,\n",
       " 0.07457733,\n",
       " 0.072834015,\n",
       " 0.07115936,\n",
       " 0.06954956,\n",
       " 0.067993164,\n",
       " 0.06650543,\n",
       " 0.06506729,\n",
       " 0.06367493,\n",
       " 0.062347412,\n",
       " 0.06106186,\n",
       " 0.05980301,\n",
       " 0.05860901,\n",
       " 0.05744934,\n",
       " 0.05632019,\n",
       " 0.055225372,\n",
       " 0.0541687,\n",
       " 0.053150177,\n",
       " 0.05215454,\n",
       " 0.05120468,\n",
       " 0.050266266,\n",
       " 0.049373627,\n",
       " 0.048488617,\n",
       " 0.04763794,\n",
       " 0.046806335,\n",
       " 0.04601288,\n",
       " 0.045230865,\n",
       " 0.04447937,\n",
       " 0.04373932,\n",
       " 0.043022156,\n",
       " 0.042316437,\n",
       " 0.041641235,\n",
       " 0.040977478,\n",
       " 0.04033661,\n",
       " 0.039699554,\n",
       " 0.039081573,\n",
       " 0.03848648,\n",
       " 0.03791046,\n",
       " 0.037330627,\n",
       " 0.036777496,\n",
       " 0.03624344,\n",
       " 0.035720825,\n",
       " 0.035194397,\n",
       " 0.034698486,\n",
       " 0.03419876,\n",
       " 0.033714294,\n",
       " 0.033241272,\n",
       " 0.03277588,\n",
       " 0.032325745,\n",
       " 0.03187561,\n",
       " 0.031448364,\n",
       " 0.031028748,\n",
       " 0.03062439,\n",
       " 0.030204773,\n",
       " 0.029815674,\n",
       " 0.029426575,\n",
       " 0.029037476,\n",
       " 0.028678894,\n",
       " 0.028320312,\n",
       " 0.027954102,\n",
       " 0.02759552,\n",
       " 0.027275085,\n",
       " 0.026916504,\n",
       " 0.02658844,\n",
       " 0.026268005,\n",
       " 0.02594757,\n",
       " 0.025627136,\n",
       " 0.02532959,\n",
       " 0.025032043,\n",
       " 0.024726868,\n",
       " 0.02444458,\n",
       " 0.024169922,\n",
       " 0.023887634,\n",
       " 0.023605347,\n",
       " 0.023338318,\n",
       " 0.023071289,\n",
       " 0.02281189,\n",
       " 0.022567749,\n",
       " 0.022315979,\n",
       " 0.022064209,\n",
       " 0.021812439,\n",
       " 0.021591187,\n",
       " 0.021347046,\n",
       " 0.021118164,\n",
       " 0.020896912,\n",
       " 0.02067566,\n",
       " 0.020454407,\n",
       " 0.020240784,\n",
       " 0.02002716,\n",
       " 0.019813538]"
      ]
     },
     "execution_count": 42,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "loss_"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {},
   "outputs": [],
   "source": [
    "tf.reset_default_graph()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[<matplotlib.lines.Line2D at 0x1ec95147da0>]"
      ]
     },
     "execution_count": 44,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAXQAAAD8CAYAAABn919SAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDIuMi4yLCBodHRwOi8vbWF0cGxvdGxpYi5vcmc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PbcQ48LSZrbboPK4Ade6+D6IfNqA2DXXF3cbJ/8nSvb/iRttHmfS5u5toTy5unpm9YWYvmtkVaahnpJ9dpuyvK4B2d982bFnK99cp+ZCyz1g2BPqY5i5NJTOrAH4CfNrde4DvAo3Au4F9RP/kS7XL3H05cAPwSTO7Mg01jMiiM1rdDPxHbFEm7K+zyYjPnZndBwwBj8YW7QPmuPsy4K+A/2tmlSksabSfXUbsL+B2Tu44pHx/jZAPozYdYVlC+ywbAj2j5i41s0KiP6xH3f2nAO7e7u5hd48A32OS/tQ8E3ffG3vsAJ6I1dAe/xMu9tiR6rpibgDWuHt7rMa0769hRttHaf/cmdmdwE3An3hs0DU2pLE/9nw10bHqRamq6Qw/u0zYXwXAHwGPx5elen+NlA+k8DOWDYGeMXOXxsbnHgQ2ufvXhy0fPu51K7Dh1O+d5LrKzSwUf070gNoGovvpzlizO4H/TGVdw5zUa0r3/jrFaPvoSeBPY2ciXAJ0x/9sTgUzux74a+Bmdz8ybHmNmQVjz+cDC4EdKaxrtJ/dk8BtZlZsZvNidf0uVXXFXAtsdvfW+IJU7q/R8oFUfsZScfQ3CUePbyR6xLgFuC+NdVxO9E+idcCbsa8bgX8H1seWPwnMSHFd84meYbAW2BjfR8A04FlgW+xxahr2WRmwH6gatiwt+4voL5V9wCDR3tE9o+0jon8Ofyf2mVsPrEhxXduJjq/GP2cPxNp+KPYzXgusAf4wxXWN+rMD7ovtry3ADamsK7b8YeATp7RN5f4aLR9S9hnTlaIiIjkiG4ZcRERkDBToIiI5QoEuIpIjFOgiIjlCgS4ikiMU6CIiOUKBLiKSIxToIiI54v8Dacw14uiiO8kAAAAASUVORK5CYII=\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "import matplotlib.pyplot as plt\n",
    "%matplotlib inline\n",
    "plt.plot(loss_)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {},
   "outputs": [],
   "source": [
    "def predict(predcit_feature, model_path):\n",
    "    embedding_size = 128\n",
    "    unit_num = 128\n",
    "    dropout_rate = None\n",
    "    output_size = 5\n",
    "    batch_size = 1\n",
    "    seq_length = 10\n",
    "    lr = 0.001\n",
    "    \n",
    "    X = tf.placeholder(tf.float32, shape=[batch_size, seq_length * embedding_size])\n",
    "    pred = lstm_crf(X)\n",
    "    saver = tf.train.Saver(tf.global_variables())\n",
    "    predict_label=[]\n",
    "    with tf.Session() as sess:\n",
    "        #参数恢复\n",
    "        module_file = tf.train.latest_checkpoint(model_path)\n",
    "        saver.restore(sess, module_file)\n",
    "        for step in range(len(predcit_feature)-1):\n",
    "            prob = sess.run(pred, feed_dict={X:predcit_feature[step]})\n",
    "            predict=prob.reshape((-1)).reshape(-1,5)\n",
    "            predict_label.append(predict)\n",
    "    return predict_label"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "INFO:tensorflow:Restoring parameters from ./NER_FOLDER/lstm_crf-180\n"
     ]
    }
   ],
   "source": [
    "predict_result = predict(mat, \"./NER_FOLDER/\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[['O' 'B-ORG-A' 'O' 'O' 'O' 'O' 'O' 'O' 'O' 'O']]\n",
      "[['O' 'O' 'O' 'O' 'O' 'O' 'O' 'O' 'B-ORG-A' 'I-ORG-A']]\n",
      "[['O' 'O' 'O' 'O' 'O' 'O' 'O' 'O' 'O' 'O']]\n",
      "[['O' 'O' 'O' 'O' 'O' 'O' 'O' 'O' 'O' 'O']]\n",
      "[['O' 'O' 'O' 'O' 'O' 'O' 'O' 'O' 'O' 'O']]\n",
      "[['O' 'O' 'O' 'O' 'O' 'O' 'O' 'O' 'O' 'O']]\n",
      "[['O' 'B-ORG-O' 'I-ORG-O' 'O' 'O' 'O' 'B-ORG-O' 'O' 'B-ORG-O' 'I-ORG-O']]\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "D:\\ProgramingTools\\Anaconda\\lib\\site-packages\\sklearn\\preprocessing\\label.py:151: DeprecationWarning: The truth value of an empty array is ambiguous. Returning False, but in future this will result in an error. Use `array.size > 0` to check that an array is not empty.\n",
      "  if diff:\n",
      "D:\\ProgramingTools\\Anaconda\\lib\\site-packages\\sklearn\\preprocessing\\label.py:151: DeprecationWarning: The truth value of an empty array is ambiguous. Returning False, but in future this will result in an error. Use `array.size > 0` to check that an array is not empty.\n",
      "  if diff:\n",
      "D:\\ProgramingTools\\Anaconda\\lib\\site-packages\\sklearn\\preprocessing\\label.py:151: DeprecationWarning: The truth value of an empty array is ambiguous. Returning False, but in future this will result in an error. Use `array.size > 0` to check that an array is not empty.\n",
      "  if diff:\n",
      "D:\\ProgramingTools\\Anaconda\\lib\\site-packages\\sklearn\\preprocessing\\label.py:151: DeprecationWarning: The truth value of an empty array is ambiguous. Returning False, but in future this will result in an error. Use `array.size > 0` to check that an array is not empty.\n",
      "  if diff:\n",
      "D:\\ProgramingTools\\Anaconda\\lib\\site-packages\\sklearn\\preprocessing\\label.py:151: DeprecationWarning: The truth value of an empty array is ambiguous. Returning False, but in future this will result in an error. Use `array.size > 0` to check that an array is not empty.\n",
      "  if diff:\n",
      "D:\\ProgramingTools\\Anaconda\\lib\\site-packages\\sklearn\\preprocessing\\label.py:151: DeprecationWarning: The truth value of an empty array is ambiguous. Returning False, but in future this will result in an error. Use `array.size > 0` to check that an array is not empty.\n",
      "  if diff:\n",
      "D:\\ProgramingTools\\Anaconda\\lib\\site-packages\\sklearn\\preprocessing\\label.py:151: DeprecationWarning: The truth value of an empty array is ambiguous. Returning False, but in future this will result in an error. Use `array.size > 0` to check that an array is not empty.\n",
      "  if diff:\n"
     ]
    }
   ],
   "source": [
    "for line in predict_result:\n",
    "    predict_label = onehot_model.decode(line)\n",
    "    print(predict_label)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
